﻿using System;
using System.Web;
using Enyim.Caching;
using Ayatta.Contract;
using Web.My.Models;
using System.Web.Mvc;
using Web.Core.Helpers;
using Web.Core.Security;
using Ayatta.Contract.Domain;
using Ayatta.Contract.Component;

namespace Web.My.Controllers
{
    public class ProfileController : BaseController
    {

        public ProfileController(Lazy<IInfrastructureService> infrastructure, Lazy<IPassportService> passport, Lazy<IAccountService> account, Lazy<IStoreService> store, Lazy<ITradeService> trade, Lazy<IPlayService> play, Lazy<IMiscService> misc, Lazy<IExtraService> extra, IMemcachedClient memcachedClient)
            : base(infrastructure, passport, account, store, trade, play, misc, extra, memcachedClient)
        {
        }

        public ActionResult Index()
        {
            var model = new ProfileControllerModel.Index();

            var identity = User.Identity.Identity();
            var user = Passport.Value.UserGet(identity.Id, true);
            model.User = user;

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(string nickname, UserProfile profile)
        {
            var result = new MagicKit();
            var identity = User.Identity.Identity();
            var user = Passport.Value.UserGet(identity.Id);
            var old = Passport.Value.UserProfileGet(identity.Id);
            if (TryUpdateModel(old))
            {
                Passport.Value.UserNicknameUpdate(identity.Id, nickname);
                var status = result.Status = Passport.Value.UserProfileUpdate(old, user.Authenticated);
                result.Status = status;
                if (!status)
                {
                    result.Message = "更新失败，请重试！";
                }
            }
            return Json(result);
        }

        /// <summary>
        /// 更新Avatar
        /// </summary>
        /// <returns></returns>
        public ActionResult Avatar()
        {
            return PartialView();
        }

        /// <summary>
        /// 更新Avatar
        /// </summary>
        /// <param name="bigFileName">大图标</param>
        /// <param name="middleFileName">中图标</param>
        /// <param name="smallFileName">小图标</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Avatar(HttpPostedFileBase bigFileName, HttpPostedFileBase middleFileName, HttpPostedFileBase smallFileName)
        {
            var status = false;
            var identity = User.Identity.Identity();

            if (bigFileName != null && middleFileName != null && smallFileName != null)
            {
                status = AvatarSave(identity.Id, AvatarSize.Big, bigFileName).Status;

                AvatarSave(identity.Id, AvatarSize.Middle, middleFileName);

                AvatarSave(identity.Id, AvatarSize.Small, smallFileName);
            }
            var result = new MagicKit<string>(status, WebHelper.GetAvatarUrl(identity.Id));
            return Json(result);
        }

        private MagicKit<string> AvatarSave(int userId, AvatarSize avatar, HttpPostedFileBase postedFile)
        {
            var fileName = string.Format("{0}.{1}.jpg", userId, avatar.GetSize());
            var result = FtpUpload(postedFile.InputStream, "/avatar/", fileName);
            if (result.Status)
            {
                if (avatar == AvatarSize.Big)
                {
                    Passport.Value.UserAvatarUpdate(userId, true);
                }
            }
            return result;
        }
    }
}
